package com.espeaker.sdk.api;

import org.json.JSONObject;

import com.espeaker.sdk.ESpeakerParameters;
import com.espeaker.sdk.NetException;
import com.espeaker.sdk.net.HttpManager;
import com.espeaker.sdk.utils.TimeUtil;
import com.espeaker.sdk.utils.Utility;

public class MiaoApi {
	private static final String LOG_TAG = MiaoApi.class.getSimpleName();

	protected static int TIME_DIFF = 0;

	public static final String MD5_BASE = "8d70e0d1acb06b4648c7aa8927509660";

	public static boolean IsDebug=false;
	
	// 接口列表
	// 登录
	protected static final String USER_LOGIN = "login";
	// 获取验证码
	protected static final String USER_GET_ACTIVE_CODE = "get-active-code";
	// 验证验证码
	protected static final String USER_VERIFIY_ACTIVE_CODE = "verify-active-code";
	// 设置密码
	protected static final String USER_SET_PASSWORD = "set-password";
	// 语音提交
	protected static final String GOLD_AUDIO_SUBMIT = "save-record";
	// 获取账户信息
	protected static final String USER_GET_COINS = "get-account-info";
	// 清除已读
	protected static final String USER_CLEAR_UNREAD= "clear-unread";
	// 获取账户信息
	protected static final String USER_QIANDAO = "qiandao";
	// 是否签到
	protected static final String USER_HASQIANDAO = "qiandao-shifou";
	//用户反馈
	protected static final String USER_FANKUI = "fankui";
	// 请求批改
	protected static final String GOLD_CHARGE = "request-xiaofei";
	// 答题思路
	protected static final String GOLD_TIPS = "tips";
	// 听力原文
	protected static final String GOLD_TingLi = "tingliyuanwen";
	// 批量请求批改
	protected static final String GOLD_MAnY_CHARGE = "request-many-xiaofei";
	// 我的批改
	protected static final String GOLD_MY_REVISERS = "get-all-xiaofei";
	// 我的批改
	protected static final String GOLD_TEACHERS = "teachers";
	// 排行榜
	protected static final String GOLD_RANKING = "paihangbang";
	// 排行榜
	protected static final String GOLD_RANKING_OTHERS = "paihangbang-others";
	// 回复
	protected static final String GOLD_FEEDBACK = "feedback";
	// 一条消费
	protected static final String GOLD_ONE_CHARGE = "get-one-xiaofei";
	// 版本更新
	protected static final String GET_VERSION = "app-update";
	//充值
	protected static final String GOLD_CHONGZHI = "chongzhi";
	
	//机经最近
	protected static final String JIJING_LATEST = "jijing-latest";
	//机经往期
	protected static final String JIJING_WANGQI = "jijing-wangqi-list";
	//机经一期
	protected static final String JIJING_ONQI = "jijing-one-qi";
	//机经一题
	protected static final String JIJING_ONTI = "jijing-one-ti";
	
	// Http Post 方法
	public static final String HTTPMETHOD_POST = "POST";
	// Http Get 方法
	public static final String HTTPMETHOD_GET = "GET";

	/**
	 * source 消息来源
	 */
	public static enum SOURCE {

		/**
		 * android
		 */
		android("android");

		public final String value;

		SOURCE(String value) {
			this.value = value;
		}
	}
	public static String GetBaseURL()
	{
		if(IsDebug)
		{
			return "http://dev.miao.yishuodian.com/";
		}else
		{
			return "http://miao.yishuodian.com/";
		}
	}
	public static boolean isRemoteActivite() {
		boolean mFlag = false;
		try {
			
			String url = MiaoApi.GetBaseURL() + "now";
			ESpeakerParameters params = new ESpeakerParameters();
			String rlt = HttpManager.openUrl(url, HTTPMETHOD_GET, params, null);
			Integer.parseInt(rlt);
			mFlag=true;
		} catch (Exception er) {
			mFlag=false;
		}
		return mFlag;
	}

	/*
	 * 获取和服务器时间差
	 */
	public static int getTimeDiff() {
		int mRes = 0;
		String url = MiaoApi.GetBaseURL() + "now";
		ESpeakerParameters params = new ESpeakerParameters();
		try {
			String rlt = HttpManager.openUrl(url, HTTPMETHOD_GET, params, null);
			System.out.println(rlt);
			int serverTime = Integer.parseInt(rlt);
			mRes = TimeUtil.getCurrentTimeDiff(serverTime);
		} catch (NetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return mRes;
	}

	/*
	 * 获取校验过的时间戳
	 */
	public static int getSeed() {
		int mRes = 0;
		if (TIME_DIFF == 0) {
			TIME_DIFF = getTimeDiff();
		}
		mRes = TimeUtil.getCheckTime(TIME_DIFF);
		return mRes;
	}

	/*
	 * 获取Token
	 */
	public static String getToken() {
		String mRes = "";
		mRes = getSeed() + MD5_BASE;
		mRes = Utility.md5(mRes);
		return mRes;
	}

	/*
	 * 获取签名 sign
	 */
	public static String getSign(JSONObject json) {
		String mRes = "";
		mRes = json + MD5_BASE;
		mRes = Utility.md5(mRes);
		return mRes;
	}

	/*
	 * 网络数据请求
	 */
	protected String request(final String url, final String api_method,
			final ESpeakerParameters params,int version) {
		String rlt = "";

		JSONObject json = new JSONObject();
		for (int i = 0; i < params.size(); i++) {
			try {
				if (params.isSign(params.getKey(i))) {
					json.put(params.getKey(i), params.getValue(i));
				}
			} catch (Exception er) {
			}
		}

		String newUrl = url + "api/student/"+version+"/" + api_method + "?seed="
				+ getSeed() + "&token=" + getToken() + "&source="
				+ SOURCE.android + "&sign=" + getSign(json);

		for (int i = 0; i < params.size(); i++) {
			try {
				if (!params.isSign(params.getKey(i))) {
					json.put(params.getKey(i), params.getValue(i));
				}
			} catch (Exception er) {
			}
		}
		try {
			rlt = HttpManager.openPostUrl(newUrl, json);

			System.out.println("<================================rlt======"
					+ rlt);
		} catch (NetException er) {
		}
		return rlt;
	}

}
